document.addEventListener('DOMContentLoaded', () => {
  const contactForm = document.getElementById('contactForm');
  const captchaImage = document.querySelector('.captcha-image');

  // 刷新验证码
  function refreshCaptcha() {
    captchaImage.src = '/captcha?' + new Date().getTime();
  }

  // 点击验证码图片刷新
  if (captchaImage) {
    captchaImage.addEventListener('click', refreshCaptcha);
  }

  // 表单验证
  if (contactForm) {
    contactForm.addEventListener('submit', (e) => {
      // 获取表单字段
      const name = document.getElementById('name').value.trim();
      const phone = document.getElementById('phone').value.trim();
      const email = document.getElementById('email').value.trim();
      const message = document.getElementById('message').value.trim();
      const captcha = document.getElementById('captcha').value.trim();

      // 简单验证
      if (!name || !phone || !email || !message || !captcha) {
        e.preventDefault();
        alert('请填写所有必填字段');
        return;
      }

      // 验证邮箱格式
      const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
      if (!emailRegex.test(email)) {
        e.preventDefault();
        alert('请输入有效的邮箱地址');
        return;
      }

      // 验证手机号格式（中国手机号）
      const phoneRegex = /^1[3-9]\d{9}$/;
      if (!phoneRegex.test(phone)) {
        e.preventDefault();
        alert('请输入有效的手机号码');
        return;
      }
    });
  }

  // 页面加载时刷新验证码
  refreshCaptcha();
}); 
